package com.lanswon.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.common.enums.ActivityStatus;
import com.lanswon.common.enums.ArticleStatus;
import com.lanswon.common.enums.ArticleType;
import com.lanswon.common.util.StringUtil;
import com.lanswon.entity.Activity;
import com.lanswon.entity.ArticleVO;
import com.lanswon.service.ActivityService;
import com.lanswon.service.UserService;

@Controller
@RequestMapping("admin/activity")
public class AdminActivityController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private ActivityService activityService;
	
	@RequestMapping("index")
	public String index(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) {
		
		
		modelMap.addAttribute("articleTypes", ArticleType.values());
		
		return "admin/activity/index";
	}
	
	/**
	 * @param currentPage
	 * @param pageNum
	 * @param type
	 * @param articleStatus
	 * @return
	 */
	@RequestMapping("/pageList")
	@ResponseBody
	public Map<String, Object> pageList(HttpServletRequest request, HttpServletResponse response,
			Integer page,Integer limit,Integer activityType,
			Integer activityStatus){
		Map<String, Object> viewMap = new HashMap<>();
		Map<String, Object> accessMap = new HashMap<>();
		List<Activity> articleList = null;
		int code = 0;
		String message = "查询成功";
		try {
			if(page==null) {
				page = 1;
			}
			if(limit==null) {
				limit = 10;
			}
			accessMap.put("activityType", activityType);
			accessMap.put("activityStatus", activityStatus);
			accessMap.put("currentResult", (page-1)*limit);
			accessMap.put("endResult", limit);
			Integer count = activityService.pageListCount(accessMap);
			articleList = activityService.pageList(accessMap);
			viewMap.put("data", articleList);
			viewMap.put("code", code);
			viewMap.put("count", count);  
			viewMap.put("message", message);
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 2);
			viewMap.put("message", "操作有误");
		}
		return viewMap;
	}
	
	@RequestMapping("activityUpdate")
	public String activityUpdate(HttpServletRequest request,HttpServletResponse response, Long id,ModelMap modelMap) {
		Activity activity = null;
		if(id != null) {
			activity = activityService.findArticleById(id);
		}
		modelMap.addAttribute("articleTypes", ArticleType.values());
		modelMap.addAttribute("activityStatuses", ActivityStatus.values());
		modelMap.addAttribute("activity", activity);
		return "admin/activity/activityUpdate";
	}
	
	
	@RequestMapping("updateActivity")
	@ResponseBody
	public Map<String, Object> updateActivity(HttpServletRequest request,HttpServletResponse response, Activity activity){
		Map<String, Object> viewMap = new HashMap<>();
		try {
			if(null==activity.getReleaseTimeStr()) {
				viewMap.put("code", 2);
				viewMap.put("msg", "保存失败");
				return viewMap;
			}
			System.out.println(activity.getReleaseTimeStr());
			if(activity.getReleaseTimeStr()!=null) {
				activity.setReleaseTime(StringUtil.strToDate(activity.getReleaseTimeStr(), "yyyy-MM-dd"));
			}
			if(activity.getId()!=null) {
				activityService.updateArticle(activity);
			}else {
				activityService.addArticle(activity);
			}
			viewMap.put("code", 1);
			viewMap.put("msg", "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 2);
			viewMap.put("msg", "保存失败");
		}
		return viewMap;
	}
	
	@RequestMapping("activityDel")
	@ResponseBody
	public Map<String, Object> activityDel(HttpServletRequest request,HttpServletResponse response,Long id){
		Map<String, Object> viewMap = new HashMap<>();
		try {
			if(id!=null) {
				activityService.deleteActivity(id);
			}
			viewMap.put("code", 1);
			viewMap.put("msg", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 1);
			viewMap.put("msg", "操作失败");
		}
		return viewMap;
	}
}
